<?php

namespace app\controller\student;

use app\BaseController;
use app\Code;
use app\dao\TrainingDao;
use think\facade\Request;
use think\facade\Log;
use app\validate\TrainingLogValidate;
use think\exception\ValidateException;

class TaskController extends BaseController
{
    // 获取今日任务
    public function todayTasks()
    {
        $memberId = request()->user['member_id'] ?? null;
//        $page = Request::get('page', 1);  // 默认页码为1
//        $tasksPerPage = 10;

        $tasks = TrainingDao::getTaskById($memberId);

        return $this->returnJson($tasks);
    }

    // 提交训练日志
    public function submitLog()
    {
        $data = Request::post();
        $memberId = request()->user['member_id'] ?? null;

        try {
            validate(TrainingLogValidate::class)->scene('submit')->check($data);
        } catch (ValidateException $e) {
            return $this->returnJson([], Code::UNPROCESSABLE_ENTITY, $e->getMessage());
        }

        try {
            $result = TrainingDao::submitLog($memberId, $data);

            Log::info("学员 {$memberId} 提交了训练日志");

            return $this->returnJson();
        } catch (\Exception $e) {
            Log::error("提交训练日志失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }
}
